package com.swpu.cbg.cnshop.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.internal.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.swpu.cbg.cnshop.R;

/**
 * Created by chenboge on 16/1/31.
 */
public class cnToolbar extends Toolbar implements View.OnClickListener{

    private LayoutInflater mInflater;
    private View mView;

    //初始化自定义toolbar的控件
    private EditText mSearchText;
    private TextView mTitleView;
    private ImageButton mRightImage;
    private Button mEditButton;
    private onRightEditButtonListener mListener;

    private int ACTION_EDIT=0;
    private int ACTION_ACHIEVE=1;


    public void setRightEditButtonListener(onRightEditButtonListener listener) {
        this.mListener = listener;
    }


    public cnToolbar(Context context) {
        this(context, null);
    }

    public cnToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public cnToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView();

        setContentInsetsAbsolute(10, 10);

        //获取自定义属性
        if (attrs != null) {
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.cnToolbar, defStyleAttr, 0);

            final Drawable rightButtonIcon = a.getDrawable(R.styleable.cnToolbar_RightButtonIcon);
            if (rightButtonIcon != null) {
                setRightButtonIcon(rightButtonIcon);
            }

            final boolean IsShowSearchView = a.getBoolean(R.styleable.cnToolbar_IsShowSearchView, false);
            if (IsShowSearchView) {
                hideContent();
                showRightButton();
                showTitle();
                hideEditButton();
            } else {
                showContent();
                hideTitle();
                hideRightButton();
                hideEditButton();
            }
            a.recycle();
        }

    }

    public void setRightButtonClickListener(OnClickListener listener) {
        mRightImage.setOnClickListener(listener);
    }

    private void setRightButtonIcon(Drawable rightButtonIcon) {
        mRightImage.setBackground(rightButtonIcon);

    }


    private void initView() {

        if (mView == null) {

            mInflater = LayoutInflater.from(getContext());

            mView = mInflater.inflate(R.layout.toolbar, null);
            mSearchText = (EditText) mView.findViewById(R.id.toolbar_searchview);
            mTitleView = (TextView) mView.findViewById(R.id.toolbar_title);
            mRightImage = (ImageButton) mView.findViewById(R.id.toolbar_rightButton);
            mEditButton = (Button) mView.findViewById(R.id.toolbar_righteditButton);
            mEditButton.setOnClickListener(this);
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

            addView(mView, lp);
        }
    }

    @Override
    public void setTitle(int resId) {
        this.setTitle(getContext().getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        initView();
        if (mTitleView != null) {
            mTitleView.setText(title);
        }
    }

    public void setEditButtonText(int resId) {
        mEditButton.setText(getContext().getText(resId));
    }


    public void setEditButtonText(CharSequence title) {
        mEditButton.setText(title);

    }


    public void showTitle() {
        mTitleView.setVisibility(VISIBLE);
    }

    public void hideTitle() {
        mTitleView.setVisibility(GONE);
    }

    public void showContent() {
        mSearchText.setVisibility(VISIBLE);
    }

    public void hideContent() {
        mSearchText.setVisibility(GONE);
    }

    public void showRightButton() {
        mRightImage.setVisibility(VISIBLE);
    }

    public void hideRightButton() {
        mRightImage.setVisibility(GONE);
    }

    public void showEditButton() {
        mEditButton.setVisibility(VISIBLE);
    }

    public void hideEditButton() {
        mEditButton.setVisibility(GONE);
    }

    public Button getmEditButton() {
        return mEditButton;
    }

    public void setmEditButton(Button mEditButton) {
        this.mEditButton = mEditButton;
    }

    @Override
    public void onClick(View v) {
        if(Integer.parseInt(v.getTag().toString())==ACTION_EDIT){
            mListener.onEditClick(v);
        }else if(Integer.parseInt(v.getTag().toString())==ACTION_ACHIEVE){
            mListener.onAchieveClick(v);
        }
    }

    public interface onRightEditButtonListener{
        void onEditClick(View view);
        void onAchieveClick(View view);
    }

}

